package com.appcenter.application.controller;


import com.appcenter.application.input.Job.GetResultJobsInput;
import com.appcenter.application.input.externalapi.GetOrgsInput;
import com.appcenter.application.input.externalapi.GetUsersInput;
import com.appcenter.application.service.ExternalAPISercive;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.config.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("services/appdata/api")
@Api(value = "管理中心向业务系统数据同步", description = "管理中心向业务系统数据同步")
public class ApplicationToBusiness {

    @Autowired
    ExternalAPISercive externalAPISercive;

    @ApiOperation(value = "获取全部人员信息", notes = "获取全部人员信息", produces = "application/json")
    @PostMapping("/getusers")
    public ResponseEntity<JsonResult> GetUsers(@RequestBody GetUsersInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();
        try {
            r.setSuccess(true);
            input.setAppCode("JZ");
            r.setResult(externalAPISercive.GetUsers(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.ok(r);
        } catch(Exception e){
            ErrorJsonResult curErrorJsonResult=new ErrorJsonResult("100",e.getMessage().toString());
            r.setError(curErrorJsonResult);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "获取全部组织架构", notes = "获取全部组织架构", produces = "application/json")
    @PostMapping("/getorgs")
    public ResponseEntity<JsonResult> GetOrgs(@RequestBody GetOrgsInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();
        try {
            r.setSuccess(true);
            input.setTile(true);
            input.setJobShow(false);
            r.setResult(externalAPISercive.GetOrgs(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.ok(r);
        } catch(Exception e){
            ErrorJsonResult curErrorJsonResult=new ErrorJsonResult("100",e.getMessage().toString());
            r.setError(curErrorJsonResult);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "获取岗位信息", notes = "获取岗位信息", produces = "application/json")
    @PostMapping("/getjobs")
    public ResponseEntity<JsonResult> GetJobs(@RequestBody GetResultJobsInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();
        try {
            r.setSuccess(true);
            input.setAppCode("JZ");
            r.setResult(externalAPISercive.GetJobs(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.ok(r);
        } catch(Exception e){
            ErrorJsonResult curErrorJsonResult=new ErrorJsonResult("100",e.getMessage().toString());
            r.setError(curErrorJsonResult);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }


}
